/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.solaris;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

/**
 *
 * @author Frank
 */
public class LocationAdapter implements LocationListener {
   
    private static LocationAdapter locationAdapter;
    private LocationManager locationManager;
    private static Activity activity;
    
    private static double latitude;  
    private static double longitude;  
    
    public LocationAdapter(Context context) {
        
        // LocationManager ophalen
        this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        
    }

    protected static LocationAdapter instantiate(Context context){
        
        if(locationAdapter == null) {
            locationAdapter = new LocationAdapter(context);
        }
        
        return locationAdapter;
        
    }
    
    public LocationManager getLocationManager() {
        return this.locationManager;
    }
    
    public boolean checkGps() {
        return this.locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }
    
    public boolean checkNetwerk(){
        return this.locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }
    
    public String getLocationAsString() {
        
        double latitude = 0;
        double lontitude = 0;
        Location location = null;
        
        try {
            
            if(this.checkGps()) {
                
                this.locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
                location = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                
            } else {
                
                if(this.checkNetwerk()) {
                    
                    this.locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
                    location = this.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    
                }
                
            }
            
            if(location != null) {
                
                // Location bepalen
                latitude = location.getLatitude();
                lontitude = location.getLongitude();
                
                String locatie = Double.toString(latitude) + " / " + Double.toString(lontitude);
                
                return locatie;
                
            }
            
        } catch(Exception e) {
            System.out.println(e); 
        }
        
        return "";
        
    }
    
    public void onLocationChanged(android.location.Location location) {
        
        location.getLatitude();  
        location.getLongitude();  
        
        this.latitude = location.getLatitude();  
        this.longitude = location.getLongitude();
        
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        
    }

    public void onProviderEnabled(String provider) {
        
    }

    public void onProviderDisabled(String provider) {
        
    }
    
    
    
}
